# frozen_string_literal: true

# FIXME: `undefined method `[]' for nil` occurs Prism 0.24.0. It has been resolved in
# the development line. This will be resolved in Prism > 0.24.0 and higher releases.
# rubocop:disable Layout/LineLength
RSpec.describe RuboCop::Cop::InternalAffairs::LocationLineEqualityComparison, :config, broken_on: :prism do
  # rubocop:enable Layout/LineLength
  it 'registers and corrects an offense when comparing `#loc.line` with LHS and RHS' do
    expect_offense(<<~RUBY)
      node.loc.line == node.parent.loc.line
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `same_line?(node, node.parent)`.
    RUBY

    expect_correction(<<~RUBY)
      same_line?(node, node.parent)
    RUBY
  end

  it 'registers and corrects an offense when comparing `#loc.source_range` with LHS and RHS' do
    expect_offense(<<~RUBY)
      node.source_range.line == node.parent.source_range.line
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `same_line?(node, node.parent)`.
    RUBY

    expect_correction(<<~RUBY)
      same_line?(node, node.parent)
    RUBY
  end

  it 'registers an offense and corrects when using `loc.first_line`' do
    expect_offense(<<~RUBY)
      node.loc.line == node.parent.loc.first_line
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `same_line?(node, node.parent)`.
    RUBY

    expect_correction(<<~RUBY)
      same_line?(node, node.parent)
    RUBY
  end

  it 'registers an offense and corrects when using `source_range.first_line`' do
    expect_offense(<<~RUBY)
      node.source_range.line == node.parent.source_range.first_line
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `same_line?(node, node.parent)`.
    RUBY

    expect_correction(<<~RUBY)
      same_line?(node, node.parent)
    RUBY
  end

  it 'registers an offense and corrects when using `first_line`' do
    expect_offense(<<~RUBY)
      node.loc.line == node.parent.first_line
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `same_line?(node, node.parent)`.
    RUBY

    expect_correction(<<~RUBY)
      same_line?(node, node.parent)
    RUBY
  end

  it 'registers an offense and corrects when using `first_line` inside block' do
    expect_offense(<<~RUBY)
      nodes.select do |node|
        node.first_line == nodes.first.first_line
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `same_line?(node, nodes.first)`.
      end
    RUBY

    expect_correction(<<~RUBY)
      nodes.select do |node|
        same_line?(node, nodes.first)
      end
    RUBY
  end

  it 'does not register an offense when using `same_line?`' do
    expect_no_offenses(<<~RUBY)
      same_line?(node, node.parent)
    RUBY
  end
end
